<?php

/**
 * This class initializes the account in the view if there is a current user logged in.
 * 
 * @author steven
 */

class CC_Controller_Action_Helper_UserSession extends Zend_Controller_Action_Helper_Abstract
{
    /** @var CC\Entity\User */
    private $_user;
    
    public function init()
    {
        $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity())
        {
            $identity = $auth->getIdentity();
            
            if($identity != '')
            {
                $em = Zend_Registry::get('doctrine')->getEntityManager();
                $this->_user = $em->getRepository('CC\Entity\User')->findOneByEmail($identity);
                
                Zend_Layout::getMvcInstance()->getView()->user = $this->_user;
            }
        }
    }
    
    /**
     * Returns the user entity if a user is currently logged in.
     */
    public function direct()
    {
        if($this->_user !== null)
            return $this->_user;
        
        return null;
    }
}
?>